package com.android.infantschool.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.infantschool.R;
import com.android.infantschool.bean.BabyRemarkBean;
import com.android.infantschool.bean.BaseBean;
import com.android.infantschool.model.config.AppConfig;
import com.android.infantschool.model.config.AppDataSource;
import com.android.infantschool.model.mvp.presenter.BabyRemarkPresenter;
import com.android.infantschool.model.mvp.presenter.BabyRemarkPresenterImpl;
import com.android.infantschool.model.mvp.view.BabyRemarkView;
import com.android.infantschool.ui.adapter.BabyRemarkAdapter;
import com.android.infantschool.ui.view.calendar.bean.DateEntity;
import com.android.infantschool.ui.view.calendar.view.DataView;
import com.android.infantschool.util.DateUtil;
import com.android.infantschool.util.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BabyRemarkActivity extends BaseActivity implements BabyRemarkAdapter.OnLister, OnRefreshListener, OnLoadMoreListener, BabyRemarkView {

    @BindView(R.id.img_back)
    public ImageView imgBack;
    @BindView(R.id.title)
    public RelativeLayout rlTitle;
    @BindView(R.id.tv_title)
    public TextView tvTitle;
    @BindView(R.id.recyclerView)
    public RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    public SmartRefreshLayout refreshLayout;
    @BindView(R.id.img_title)
    public ImageView imgTitle;
    @BindView(R.id.week)
    public DataView week;

    public BabyRemarkAdapter adapter;

    private BabyRemarkPresenter babyRemarkPresenter = null;

    public static Intent newIntent(Activity activity) {
        Intent intent = new Intent(activity, BabyRemarkActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_baby_remark);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        babyRemarkPresenter = new BabyRemarkPresenterImpl(this);
        rlTitle.setPadding(0, this.height, 0, 0);
        tvTitle.setText("宝宝评语");
        adapter = new BabyRemarkAdapter(this, this);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        recyclerView.setAdapter(adapter);

        //设置 Footer 为 球脉冲 样式
        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        refreshLayout.setRefreshFooter(new ClassicsFooter(this));
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);

        Map<String, String> map = new HashMap<>();
        map.put("token", AppConfig.getToken());
        map.put("day", DateUtil.getDate(DateUtil.ymd));
        Log.e(TAG, "map:" + map.toString());
        babyRemarkPresenter.getBabyRemark(map);

        week.setOnSelectListener(new DataView.OnSelectListener() {
            @Override
            public void onSelected(DateEntity date) {
                Map<String, String> map = new HashMap<>();
                map.put("token", AppConfig.getToken());
                map.put("day", date.date);
                Log.e(TAG, "map:" + map.toString());
                babyRemarkPresenter.getBabyRemark(map);
                BabyRemarkActivity.this.showLoading();
            }
        });
        week.getData(DateUtil.getDate(DateUtil.ymd));
    }

    @Override
    public void lister(int position) {
        BabyRemarkBean.DataBean dataBean = adapter.list.get(position);
        startActivity(NewBabyRemarkActivity.newIntent(this, dataBean.getBabyName(), String.valueOf(dataBean.getId())));
    }

    @OnClick({R.id.img_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                this.finish();
                break;
        }
    }

    @Override
    public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshLayout.finishLoadMore(true);
                refreshLayout.finishRefresh(true);
            }
        }, 1000);
    }

    @Override
    public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshLayout.finishLoadMore(true);
                refreshLayout.finishRefresh(true);
            }
        }, 1000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (babyRemarkPresenter != null) {
            babyRemarkPresenter.onDestroy();
            babyRemarkPresenter = null;
        }
    }

    @Override
    public void errorBabyRemark(String error) {
        this.dismissLoading();
        ToastUtil.showShortToast(this, "网络异常:" + error);
        adapter.clear();
    }

    @Override
    public void successBabyRemark(BaseBean baseBean) {
        this.dismissLoading();
        BabyRemarkBean babyRemarkBean = (BabyRemarkBean) baseBean;
        if (babyRemarkBean.getCode() == 0) {
            adapter.setData(babyRemarkBean.getData());
        } else {
            adapter.clear();
            ToastUtil.showShortToast(this, babyRemarkBean.getMsg());
        }
    }
}